package fgame.controller.state
{
	import fgame.controller.Controller;
	import fgame.unit.Actor3D;
	
	import flare.core.Pivot3D;
	import flare.system.Input3D;

	public class MoveState extends State
	{
		public function MoveState(controller:Controller=null)
		{
			super(controller);
			this._types=1;
			currState=new Object();
			lastState=new Object();
			graphic=new Pivot3D();
		}
		
		
		override public function begin():void
		{
			actor3D=controller.target as Actor3D;
			
			graphic.copyTransformFrom(actor3D.graphic as Pivot3D);
			
//			graphic=actor3D.graphic as Pivot3D;
			super.begin();
			lastTime=_beginTime;
			currState=getKeyState();
			recordeLast();
			calcSpeed();
			synchrostep(0);
		}
		
		override public function action(time:Number):void
		{
			calcSpeed();
			var keyState:Boolean=checkStateChange();
			
			if(currState["c"])
				direction -= 6;
			
			if(currState["d"])
				direction += 6;
			
			direction *= 0.9;

			var roy:Number= direction / 15;
			graphic.rotateY(roy);
			
			if(keyState)//如果状态改变
			{
				recordeLast();
				
				if(currState["a"] == false &&
					currState["b"] == false &&
					currState["c"] == false &&
					currState["d"] == false)
				{
					calcSpeed();
//					speed=-this.controller.target.data.speed/1000;
					synchrostep(3);
					this.complete();
				}else
				{
					synchrostep(2);

				}
			}else
			{
				var dot:Number=time-lastTime;
				lastTime=time;
				lastDtTime+=dot;
				
				this.graphic.translateZ(speed*dot);

				var dely:Number=this.controller.target.netDelay;
				if(lastDtTime>(clock-dely))
				{
					lastDtTime=0;
					synchrostep(1);
				}
				
			}
			
		}

		/**
		 * 同步位置信息
		 */
		protected function synchrostep(state:int=0):void
		{
			
			var msg:Object=new Object();
			msg.v=speed;
			msg.p=graphic.getPosition();
			msg.r=graphic.getRotation();
			msg.clock=clock;
			msg.state=state;
//			Debug.traceMe(Pivot3D(actor3D.graphic).getPosition()+","+Pivot3D(actor3D.graphic).getRotation()+"=="+msg.p+"，"+msg.r,"位置对比")

//			MsgHelper.sendActMsg(controller.target.name,this.types,msg);

		}
		
		protected function checkStateChange():Boolean
		{
			currState=getKeyState();
			if(currState["a"] == lastState["a"] &&
				currState["b"] == lastState["b"] &&
				currState["c"] == lastState["c"] &&
				currState["d"] == lastState["d"])
				return false;
			return true;
		}
		
		protected function getKeyState():Object
		{
			currState["a"]=Input3D.keyDown( Input3D.UP );
			currState["b"]=Input3D.keyDown( Input3D.DOWN );
			currState["c"]=Input3D.keyDown( Input3D.LEFT );
			currState["d"]=Input3D.keyDown( Input3D.RIGHT );
			currState
			return currState;
		}
		
		protected function calcSpeed():void
		{
			if(Input3D.keyDown( Input3D.UP ))
				speed=-this.controller.target.data.speed/1000;
			else if(Input3D.keyDown( Input3D.DOWN ))
				speed=this.controller.target.data.speed/1000;
			else
				speed=0;	
				
		}
		
		
		protected function recordeLast():void
		{
			lastState["a"]=currState["a"];
			lastState["b"]=currState["b"];
			lastState["c"]=currState["c"];
			lastState["d"]=currState["d"];

		}
		
		
		
		private var lastState:Object;
		private var currState:Object;
		
		private var lastTime:Number,lastDtTime:Number;
		
		private var speed:Number=0,direction:Number=0;
		private var clock:int=500;
		
		
	}
}